<?php
namespace app\distribution\model;
use app\BaseModel;
use app\member\model\UsersModel;
use think\Db;
use think\facade\Cache;
//*------------------------------------------------------ */
//-- 分销身份表
/*------------------------------------------------------ */
class DividendRoleModel extends BaseModel
{
	protected $table = 'distribution_dividend_role';
	public  $pk = 'role_id';
	protected static $mkey = 'distribution_dividend_role_list';
	
	 /*------------------------------------------------------ */
	//-- 清除缓存
	/*------------------------------------------------------ */ 
	public function cleanMemcache(){
		Cache::rm(self::$mkey);
	}
	/*------------------------------------------------------ */
	//-- 获取列表
	/*------------------------------------------------------ */ 
	public  function getRows(){
		$data = Cache::get(self::$mkey);
		if (empty($data) == false){
			return $data;
		}
		$rows = $this->field('*,role_id as id,role_name as name')->order('level ASC')->select()->toArray();		
		foreach ($rows as $row){
            $row['upleve_value'] = json_decode($row['upleve_value'],true);
			$data[$row['role_id']] = $row;
		}
		Cache::set(self::$mkey,$data,600);
		return $data;
	}
	/*------------------------------------------------------ */
	//-- 获取角色信息
	/*------------------------------------------------------ */ 
	public function info($role_id,$returnName = false){
		$rows = $this->getRows();
		$rows[0] = ['role_name'=>'粉丝','role_id'=>0,'level'=>0];
		if ($returnName == true){
			return $rows[$role_id]['role_name'];
		}
		return $rows[$role_id];
	}

	//用户升级
	public function uplevel($user_id){
		$rows = $this->field('*,role_id as id,role_name as name')->order('level ASC')->select()->toArray();	
		$l_user_id = $user_id;
		while(1){
			$user = (new UsersModel())->Info($l_user_id);
			if(empty($user)){
				return;
			}
			foreach ($rows as $row){
				if($user['role_id']< $row['role_id']){
					// 是否符合条件升级
					$up = 1;
					$updetail = json_decode($row['upleve_value']); 
					// 购买商品是否满足
					if(isset($updetail->buy_goods)){
						$up2 = 0;
						foreach($updetail->buy_goods as $buy_goodsv){
							$buyshop = Db::table('shop_order_info')
							->where('user_id',$user['user_id'])
							->where('FIND_IN_SET(:buy_goodsv,buy_goods_id)',['buy_goodsv'=>$buy_goodsv])
							->count('order_id');
							if($buyshop) $up2 = 1;
						}
						if($up2 == 0) $up = 0;
					}
					// 业绩是否满足
					if($updetail->allyj > 0){
						if(($user['total_consume']+$user['total_consume_td']) < $updetail->allyj){
							$up = 0;
						}
					}
					if($up == 1){
						(new UsersModel())->upInfo($user['user_id'],['role_id'=>$row['role_id']]);
					}
					if($up == 0) break;
				}
			}
			$l_user_id = $user['pid'];
			if(empty($l_user_id)) return;
		}

	}

	//加业绩
	//用户丶user_id，
	//业绩丶yj
	public function adduseryj($user_id,$yj){
		$user = Db::table('users')->where('user_id',$user_id)->find();
		if(empty($user)) return;
		Db::table('users')->where('user_id',$user_id)->update(['total_consume'=>$user['total_consume']+$yj]);
		if(empty($user['pid'])) return;
		$adduser_id = $user['pid'];
		while(1){
			$adduser = Db::table('users')->where('user_id',$adduser_id)->find();
			if(empty($user)) return;
			Db::table('users')->where('user_id',$adduser_id)->update(['total_consume'=>$adduser['total_consume']+$yj]);
			if(empty($adduser['pid'])) return;
			$adduser_id = $adduser['pid'];
		}
	}
}
